package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.UserMsg;
import com.tbit.uqbike.client.service.FeeAppealService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.service.UserMsgService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户消息
 *
 * @Author: chen
 * @Description: 用户消息
 * @Date: 创建于 11:06 2020/10/28
 */
@RestController
@RequestMapping("/userMsg")
public class UserMsgController {
    @Autowired
    private UserMsgService userMsgService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FeeAppealService feeAppealService;

    /**
     * 分页查询所有用户消息
     *
     * @param token    鉴权token
     * @param msgState 消息状态
     * @param msgType  消息类型
     * @param pageNO   页码
     * @param rowCount 每页大小
     * @param request
     * @param response
     */
    @RequestMapping("/queryPage")
    public void queryPage(String token, Integer msgState, Integer msgType, Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount,
                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            List<UserMsg> list = userMsgService.queryPage(user.getUserId(), msgState, msgType, pageNO, rowCount);
            Integer total = userMsgService.queryCount(user.getUserId(), msgState, msgType);
            map.put("total", total);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, list);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取未读消息数
     *
     * @param token
     * @param msgState 消息状态
     * @param msgType  消息类型
     * @param request
     * @param response
     */
    @RequestMapping("/getNotRead")
    public void getNotRead(String token, @RequestParam(defaultValue = "0") Integer msgState, Integer msgType,
                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            Integer total = userMsgService.queryCount(user.getUserId(), msgState, msgType);
            Integer count = feeAppealService.getNotRead(user.getUserId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, total);
            map.put("count", count);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 阅读消息
     *
     * @param userMsgId 主键id
     * @param request
     * @param response
     */
    @RequestMapping("/read")
    public void edit(Integer userMsgId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Assert.notNull(userMsgId, "-180001", "消息id不能为空");
            UserMsg userMsg = new UserMsg();
            userMsg.setUserMsgId(userMsgId);
            userMsgService.edit(userMsg);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
